declare var wx: any;
import { _decorator, resources } from 'cc';
import * as fgui from "fairygui-cc";
import AbstractUIWindow from '../../window/AbstractUIWindow';
import UICore from '../../../../Core/Res/UICore';
import SceneManager from '../SceneManager';
import GameScenes from '../GameScenes';
import TimerMgr from '../../../../Core/Timer/TimerMgr';
import SdkTool from '../../../../Core/Sdk/SdkTool';
import EventMgr from '../../../../Core/Event/EventMgr';
import { ViewManager } from '../../base/ViewManager';
import { GameModels } from '../../socket/GameModels';
import { fail } from 'assert';
import ModuleWindow from '../../window/ModuleWindow';
import WindowObject from '../../window/WindowObject';
import { ExternalGLoader } from '../../../../Core/Layer/ExternalGLoader';
import Log from '../../../../Core/Log/Log';
export class WxStartView extends AbstractUIWindow {
    private progress: fgui.GProgressBar;
    private bg_full: ExternalGLoader;
    protected getResList(): Array<string> {
        return ["Start"];
    }
    protected onInit(): void {
        this._view = UICore.createObject("Start", "WxStartView").asCom;
    }
    protected onShow(): void {
        this.bg_full.url = `bg2.jpg`;
        EventMgr.on(`event_RespEnterLobby`, this.enterGame, this);
        EventMgr.on(`wxLogin`, this.wxLogin, this);
        EventMgr.on(`serverError`, this.serverError);
        this.progress.visible = false;
        this.progress.max = 100;
        this.progress.value = 0;
        let res = [
            "UI/Common",  //描述文件
            "UI/Common_atlas0", //纹理集
        ];
        Log.ins.log(1, `开始加载Common包`);
        resources.load(res, (err, assets) => {
            Log.ins.log(1, `Common包加载完成`);
            UICore.addPackage(`Common`);
            SdkTool.ins.sdk.login();
        });
    }
    private serverError(): void {
        ModuleWindow.ins.openWindow(WindowObject.ins.AlertWindow, `服务器正在维护中，请稍后再试`);
    }
    private login(): void {
        this.progress.visible = true;
        this.progress.value = 0;
        this.progress.max = 100;
        let res = [
            "UI/Main",  //描述文件
            "UI/Main_atlas0", //纹理集
            "UI/Plane",  //描述文件
            "UI/Plane_atlas0", //纹理集
            "UI/Font",  //描述文件
            "UI/Font_atlas0", //纹理集
        ];
        Log.ins.log(1, `开始加载游戏主体资源`);
        resources.load(res, (finsh, toal, item) => {
            this.progress.max = toal;
            this.progress.value = finsh;
        }, (err, assets) => {
            Log.ins.log(1, `游戏主体资源加载完毕`);
            UICore.addPackage(`Main`);
            UICore.addPackage(`Plane`);
            UICore.addPackage(`Common`);
            UICore.addPackage(`Font`);
            SceneManager.ins.enterScene(GameScenes.main);
        });
    }
    private enterGame(): void {
        wx.getSetting({
            success: (res) => {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                    wx.getUserInfo({
                        success: (res) => {
                            Log.ins.log(2, `玩家已授权个人信息`);
                            Log.ins.log(2, `玩家昵称:${res.userInfo.nickName}`);
                            Log.ins.log(2, `玩家头像:${res.userInfo.avatarUrl}`);
                            SdkTool.ins.nickName = res.userInfo.nickName;
                            SdkTool.ins.avatarUrl = res.userInfo.avatarUrl;
                            if (SdkTool.ins.isFirst) {
                                GameModels.lobby.send_ReqUploadWXInfo();
                            }
                            this.login();
                        }
                    })
                } else {
                    // 否则，先通过 wx.createUserInfoButton 接口发起授权
                    Log.ins.log(1, `玩家未授权个人信息，创建授权按钮`);

                    const view = wx.getWindowInfo();
                    let button = wx.createUserInfoButton({
                        type: 'text',
                        text: '授权登录',
                        style: {
                            left: view.screenWidth / 2 - 100,
                            top: view.screenHeight / 2 + 100,
                            width: 200,
                            height: 40,
                            lineHeight: 40,
                            backgroundColor: '#ff0000',
                            color: '#ffffff',
                            textAlign: 'center',
                            fontSize: 16,
                            borderRadius: 4
                        }
                    })
                    button.onTap((res) => {
                        // 用户同意授权后回调，通过回调可获取用户头像昵称信息
                        if (res.userInfo) {
                            Log.ins.log(2, `玩家同意授权`);
                            Log.ins.log(2, `玩家昵称:${res.userInfo.nickName}`);
                            Log.ins.log(2, `玩家头像:${res.userInfo.avatarUrl}`);
                            SdkTool.ins.nickName = res.userInfo.nickName;
                            SdkTool.ins.avatarUrl = res.userInfo.avatarUrl;
                            if (SdkTool.ins.isFirst) {
                                GameModels.lobby.send_ReqUploadWXInfo();
                            }
                            this.login();
                            button.destroy();
                        } else {
                            Log.ins.log(1, `玩家未同意授权`);
                            ViewManager.ins.showtips(`授权失败，请重新登录`);
                        }
                    })
                }
            }
        });
    }
    private wxLogin(isSuccess: boolean): void {
        if (!isSuccess) {
            ViewManager.ins.showtips(`登录失败，请重新登录`);
            return;
        }


    }
}


